package developer.算法.二叉树.二叉树搜索树第K小的数值;

import java.util.*;

/**
 * @author zhangyongkang
 * @time 2025/3/26 10:44
 * @description 给定一个二叉搜索树的根节点 root ，和一个整数 k ，请你设计一个算法查找其中第 k 小的元素（从 1 开始计数）。
 * <p>
 * 输入：root = [3,1,4,null,2], k = 1
 * 输出：1
 * <p>
 * 输入：root = [5,3,6,2,4,null,null,1], k = 3
 * 输出：3
 * <p>
 * 1
 * 2   3
 */
public class ErChaSouSuoShuDiKXiaoDeShuZi {

    public static void main(String[] args) {
    }

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {},
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */

    static class Solution4 {

        public int kthSmallest(TreeNode root, int k) {
            Stack<TreeNode> stack = new Stack<>();

            while (root != null || !stack.isEmpty()) {//这里是精髓
                while (root != null) {
                    stack.push(root);
                    root = root.left;
                }
                TreeNode node = stack.pop();
                if (--k == 0) {
                    return node.val;
                }
                root = node.right;//精髓
            }
            return root.val;
        }
    }


    static class Solution3 {
        int num;

        public int kthSmallest(TreeNode root, int k) {
            Stack<TreeNode> stack = new Stack<>();

            while (root != null || !stack.isEmpty()) {
                while (root != null) {
                    stack.push(root);//中序遍历 左中右
                    root = root.left;//到最左边的元素
                }
                TreeNode node = stack.pop();//弹栈
                if (--k == 0) {
                    return node.val;
                }

                root = node.right;
            }
            return root.val;
        }
    }


    class Solution {
        public int kthSmallest(TreeNode root, int k) {
            List<Integer> result = new ArrayList<>();
            loop(root, result);
            return result.get(k - 1);
        }

        public void loop(TreeNode root, List<Integer> vals) {
            if (root == null) return;
            loop(root.left, vals);
            vals.add(root.val);
            loop(root.right, vals);
        }
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class SolutionOfficial {
        public int kthSmallest(TreeNode root, int k) {
            Deque<TreeNode> stack = new ArrayDeque<>();
            while (root != null || !stack.isEmpty()) {
                while (root != null) {
                    stack.push(root);
                    root = root.left;
                }
                root = stack.pop();
                --k;
                if (k == 0) {
                    break;
                }
                root = root.right;
            }
            return root.val;
        }
    }

}
